ASP.Net & C# Create HTML Controls Dynamically
It is very easy to create HTML controls before running a Web Page. Just Drag and Drop from ToolBox or write HTML code or ASP code for that Web Page (.aspx). But people think that generating HTML controls during runtime is difficult. But it’s quite easy.
There are many methods of generating controls during runtime.
Here is the one.
First thing is that you need to add a Panel by dragging and dropping it from the toolbox.
After that you need to write the C# code in Page Load event or button event as follows :
Panel1.Controls.Add(new LiteralControl(“<table><tr>”));
Panel1.Controls.Add(new LiteralControl(“<td colspan=2 align=center valign=middle >”));
Panel1.Controls.Add(new LiteralControl(“<table><tr><td>”));
Panel1.Controls.Add(new LiteralControl(“<input id=’Text1′ type=’text’ value=’Ganesh Mohan’ />”));
Panel1.Controls.Add(new LiteralControl(“</td><td>”));
Panel1.Controls.Add(new LiteralControl(“<a href=https://ganeshmohan.wordpress.com>”));
Panel1.Controls.Add(new LiteralControl(“Hi This is Ganesh”));
Panel1.Controls.Add(new LiteralControl(“</a>”));
Panel1.Controls.Add(new LiteralControl(“</td></tr></table>”));
That’s it. Its all Done. Its So Simple isn’t it!
This code doesn’t work !!
Please fix !!
the double qoutes are not recognized as valid c# syntax!
public partial class SeatingChartLayout : System.Web.UI.Page
{
HiddenField HiddenFieldSection;
protected void Page_PreInit(object sender, EventArgs e)
{
//Create HiddenField Control
HiddenFieldSection = new HiddenField();
HiddenFieldSection.ID = “HiddenFieldSection”;
this.form1.Controls.Add(HiddenFieldSection);
Literal br = new Literal();
br.Text = “”;
this.form1.Controls.Add(br);
}
//Dynamically is awesome!
that is a different quotation symbol. u need to use normal qoutes like ” “. it only can recognized by programming languages not the above mentioned in the code. “ ” this quotes can be generated by some IDE’s while designing this page.
I got it to work, I had to change your fonted double-quotes to C# double quotes, in other words, the font on this page is not recognized in c-sharp.
Yes… you are correct… it happens sometimes when you directly copy paste the code from here… you have to edit the double quotes since the editor does not consider it as double quote… its not my mistake…
hi i need code for generating table dynamically and display details from database.
Nice, but now how do you read the data from those controls, if it was TaxtBoxes, how would you get the user inputs using C#?
Hi sibusiso,
Good question, You need to use embedded C# code to do this. you have to use textbox id to get its value. plz check it out.
Regards
Ganesh
Hi Ganesh,
Let suppose you generate an unknown number of textbox when the button is clicked multiple times, how do you retreive all the text in codebehind?
Regards
Steve
i want to generate dynamically textbox in asp.net
it is possible.
just add textbox to panel
Jewel wt ever u do u vl not fnsh ur project,..
Yes, but how would you dynamically create server side controls such as ? This would be could so that AJAX could be implimented. For example, this code will create a dynamic table, but only
for (int i = 0; i < tblRows; i++)
{
TableRow tr = new TableRow();
for (int j = 0; j < tblCols; j++)
{
TableCell tc = new TableCell();
TextBox txtBox = new TextBox();
txtBox.Text = "RowNo:" + i + " " + "ColumnNo:" + " " + j;
Type t = typeof(TextBox);
// Add the control to the TableCell
tc.Controls.Add(txtBox);
// Add the TableCell to the TableRow
tr.Cells.Add(tc);
}
// Add the TableRow to the Table
tbl.Rows.Add(tr);
}
i want to trap the values obtained from the text boxes that is dynamically generating…
Hye Ganesh,
I want to know about that if we add all the controls in a panel. and i want to give page break at any specific position inside the panel at the time of printing this panel. so how i specify the page break in certain position. you have any idea of that. if you have please explain me how.
Hye Ganesh,
I want to know about that if we add all the controls in a panel. and i want to give page break at any specific position inside the panel at the time of printing this panel. so how i specify the page break in certain position. you have any idea of that. if you have please explain me how its possible.
You can use placeholders.
if (text == “Yes”)
{
Literal href4 = new Literal();
href4.Text = “  ”;
PlaceHolderMain.Controls.Add(href4);
Literal br = new Literal();
br.Text = “”;
PlaceHolderMain.Controls.Add(br);
}
else
{
Literal href4 = new Literal();
href4.Text = “    ”;
PlaceHolderMain.Controls.Add(href4);
Literal br = new Literal();
br.Text = “”;
PlaceHolderMain.Controls.Add(br);
}
plz let me know how generate event for dynamic table………
Button btnSubmit;
btnSubmit = new Button();
btnSubmit.ID = “btnSubmit”;
btnSubmit.Text = “Save Changes”;
btnSubmit.ForeColor = colorWhite;
btnSubmit.BackColor = color;
btnSubmit.Click += new System.EventHandler(btnSubmit_click);
PlaceHolderMain.Controls.Add(btnSubmit);
Hi Ganesh,
your article is really nice.
well can i add buttons using literal control and attach events to it?
Thanks,
Mehul Makwana
Yes You can… 🙂
Thanks for this nice article..
how to make a gridview using C# coding according to the selected item in the dropdownlist …
for (int i = 0; i < tblRows; i++) { TableRow tr = new TableRow(); for (int j = 0; j < tblCols; j++) { TableCell tc = new TableCell(); TextBox txtBox = new TextBox(); txtBox.Text = "RowNo:" + i + " " + "ColumnNo:" + " " + j; Type t = typeof(TextBox); // Add the control to the TableCell tc.Controls.Add(txtBox); // Add the TableCell to the TableRow tr.Cells.Add(tc);
+1
Thank you so much for ur reply.. its working fine for me..
hello my firend
this page very very usefull and tanks for this page
Hi,
Linear control doesnot work for Windows application.Can you please tell me what will be used for window application to create the table dynamically
Hi Ganesh,
This code is a real help…
Could you please inform on how to retrieve values from the objects created trough Literal Control?
Example: If you create a checkbox for each row, to allow the user to select a line, how would you retrieve the value or wich checkboxes were chosen by the user????
Thanks
I want to create a button i which any user clicks on the button the next button will automatically view and if the user clicks on the generated button the another button will automatically generate as done In Gmail Label setting . Will u please help me out.
thanks.. your code helping a lot.. good job..
vinay
Dear Ganesh,
Albert Einstein famously stated: “Everything should be made as simple as possible, but no simpler.” I believe you have accomplished this.
Thank you & Sat Namm
Karam Prem Singh
Panel1.Controls.Add(new LiteralControl(“”));
Panel1.Controls.Add(new LiteralControl(“”));
Panel1.Controls.Add(new LiteralControl(“”));
Panel1.Controls.Add(new LiteralControl(“”));
Panel1.Controls.Add(new LiteralControl(“”));
Panel1.Controls.Add(new LiteralControl(“”));
Panel1.Controls.Add(new LiteralControl(“Hi This is Ganesh”));
Panel1.Controls.Add(new LiteralControl(“”));
Panel1.Controls.Add(new LiteralControl(“”));
this code is not working
you can try this for make runtime table :
DataSet dsTemp = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillGrid();
}
}
protected void FillGrid()
{
dsTemp.Tables.Add(“Sales”);
dsTemp.Tables[0].Columns.Add(“ID”, typeof(int));
dsTemp.Tables[0].Columns.Add(“Name”, typeof(string));
DataRow dr = dsTemp.Tables[0].NewRow();
dr[“ID”] = 1;
dr[“Name”] = “Sales Invoice”;
dsTemp.Tables[0].Rows.Add(dr);
DataRow dr1 = dsTemp.Tables[0].NewRow();
dr[“ID”] = 2;
dr[“Name”] = “Purchase Invoice”;
dsTemp.Tables[0].Rows.Add(dr1);
GridView1.DataSource = dsTemp.Tables[0];
GridView1.DataBind();
}
you can make in grid
anybody can make it in form also
it is working proper i try it
thanks
Hi
I want to create a dynamic controls and save it these dynamic controls in another form.
i can able to create dynamic controls. but i dont know how to save these controls in another new form.
can u please help me out?
Very informative post. It’s really helpful for me and helped me lot to complete my task. Thanks for sharing with us. I had found another nice post over the internet which was also explained very well about , for more details of this post check out this link…
http://www.mindstick.com/Articles/72cc1085-93d0-4f80-adf4-ffdc3d6e8014/?Literal%20Control%20in%20ASP.Net
Thanks
Very informative post. It’s really helpful for me and helped me lot to complete my task. Thanks for sharing with us. I had found another nice post over the internet which was also explained very well about Populate Grid Control From XML Document Easily, for more details of this post check out this link…..
http://www.mindstick.com/Articles/08f2f90b-71e9-4a31-abf0-5131e3c19f58/?HiddenField%20Control%20in%20ASP.Net
Thanks everyone for your precious post!!
This is very informative post which is helps me at the time of website development. I am thankful to you. Keep it up….
Hi
This works great … How can I use it to create multiple controls? i.e. I want to repeatedly use a putton to add tables or rows … this code works only once? After that, if you click the button again, nothing happens.
Can you help?
Thank you.
Les 🙂
sir i want to know about the control of .net frame work can u give something that i can quick revise them and play with them…plz replay me
thanks
Thank you.
Do you mind if I quote a couple of your articles as long as I
provide credit and sources back to your website?
My website is in the very same niche as yours and my users would truly benefit from some of the information you
present here. Please let me know if this okay with you. Appreciate it!
No problem… make sure there should be no spams…
It is possible to dynamically create a .NET LiteralControl, and instantiate it with the generated HTML of an already existing, statically-created web UI control (for instance, an ), But what I want to know is: is there a way to then refer to and manipulate this newly-created LiteralControl as an instance of the .NET control that was contained in that stream of HTML mark-up? In other words, after I create the LiteralControl, is it possible for the program to treat it as an , and modify the contents of its sub-controls, without having to deal directly with the text inside that HTML mark-up?
Asking questions are genuinely fastidious thing if you are not understanding anything completely,
except this piece of writing presents nice understanding yet.
It’s truly a great and helpful piece of information. I’m
satisfied that you shared this useful information with us.
Please keep us up to date like this. Thank you for sharing.
I want to fix those textboxes and labels permanently after generating once…how can i do that….there were lot of solution for how to create dyamic controls….but i want them as it is after once generating…..
It is Working Good…